package com.wallet.crypto.trustapp.ui.setting;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wallet.crypto.trustapp.R;
import com.wallet.crypto.trustapp.ui.BaseActivity;
import com.wallet.crypto.trustapp.ui.widget.adapter.PeakAdapter;
import com.wallet.crypto.trustapp.ui.widget.adapter.SettingAdapter;
import com.wallet.crypto.trustapp.ui.widget.entity.SettingItem;
import com.wallet.crypto.trustapp.util.Loger;
import com.wallet.crypto.trustapp.widget.CustomDialog;
import com.wallet.crypto.trustapp.widget.SpacesItemDecoration;

import java.util.ArrayList;

public class SettingPeakActivity extends BaseActivity {
    private static final int REQUEST_CODE = 0;
    private static final int[] ITEM_TITLE = {
            R.string.s0018,
            R.string.s0019,
            R.string.s0020,
            R.string.s0021
    };

    private final ArrayList<SettingItem> mItems = new ArrayList<>();

    private ImageView mClose;
    private TextView mTitle;
    private Button mDelete;
    private RecyclerView mRecyclerView;
    private PeakAdapter mAdapter;
    private CustomDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        initView();
    }

    private void initView() {
        mClose = findViewById(R.id.top_icon);
        mTitle = findViewById(R.id.top_title);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(R.string.s0005);
        mDelete = findViewById(R.id.delete_data);
        mDelete.setVisibility(View.VISIBLE);
        mDelete.setText(R.string.s0032);
        mRecyclerView = findViewById(R.id.setting_rev);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(10));
        initData();
        mAdapter = new PeakAdapter(this, mItems);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setmOnItemClickListener((view, position) -> {

        });
        mClose.setOnClickListener(this::OnCloseListener);
        mDelete.setOnClickListener(this::OnDeleteListener);
    }

    //删除数据
    private void OnDeleteListener(View view) {
        createCare();
    }

    //创建注意Dialog
    private void createCare() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        mDialog = builder.cancelTouchout(false)
                .view(R.layout.dialog_care)
                .widthdp(300)
                .addViewOnclick(R.id.button_one, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(SettingPeakActivity.this, ChangePINActivity.class);
                        intent.putExtra("isAmend", false);
                        intent.putExtra("tag", 1);
                        startActivityForResult(intent, REQUEST_CODE);
                        mDialog.dismiss();
                    }
                }).addViewOnclick(R.id.button_two, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                }).build();
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mDialog = null;
            }
        });
        mDialog.show();
    }

    //创建初始化完成
    private void createComplete() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        mDialog = builder.cancelTouchout(false)
                .view(R.layout.dialog_peak)
                .widthdp(300)
                .addViewOnclick(R.id.button, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mDialog.dismiss();
                    }
                })
                .build();
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mDialog = null;
            }
        });
        mDialog.show();
    }

    //关闭界面
    private void OnCloseListener(View view) {
        finish();
    }

    private void initData() {
        mItems.add(new SettingItem(ITEM_TITLE[0], 0));
        mItems.add(new SettingItem(ITEM_TITLE[1], 0));
        mItems.add(new SettingItem(0, 1));
        mItems.add(new SettingItem(ITEM_TITLE[2], 2));
        mItems.add(new SettingItem(ITEM_TITLE[3], 2));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE == 0) {
            switch (resultCode) {
                case 0:
                    createCare();
                    break;
                case 1:
                    createComplete();
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }
}
